package cn.edu.ncut.cs.springboot.petmanagementsystem.controller;

import cn.edu.ncut.cs.springboot.petmanagementsystem.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/common")
@Tag(name = "公共接口模块")
public class CommonController {

    @Operation(summary = "上传图片或文件（宠物照片、健康报告等）")
    @PostMapping("/upload")
    public Result<?> uploadFile(
            @Parameter(description = "文件") @RequestParam("file") MultipartFile file) {
        try {
            if (file.isEmpty()) {
                return Result.error("文件不能为空");
            }
            String fileUrl = "/uploads/" + System.currentTimeMillis() + "_" + file.getOriginalFilename();
            Map<String, Object> result = new HashMap<>();
            result.put("fileUrl", fileUrl);
            result.put("fileName", file.getOriginalFilename());
            result.put("fileSize", file.getSize());
            return Result.success("上传成功", result);
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return Result.error("上传失败: " + e.getMessage());
        }
    }

    @Operation(summary = "下载文件")
    @GetMapping("/download/{fileId}")
    public Result<?> downloadFile(
            @Parameter(description = "文件ID") @PathVariable String fileId) {
        try {
            Map<String, Object> result = new HashMap<>();
            result.put("fileId", fileId);
            result.put("downloadUrl", "/files/" + fileId);
            return Result.success(result);
        } catch (Exception e) {
            log.error("下载文件失败", e);
            return Result.error("下载失败: " + e.getMessage());
        }
    }

    @Operation(summary = "获取字典信息（如宠物种类、服务类型等）")
    @GetMapping("/dict")
    public Result<?> getDict(
            @Parameter(description = "字典类型") @RequestParam(required = false) String type) {
        try {
            Map<String, Object> dict = new HashMap<>();
            dict.put("petTypes", new String[]{"猫", "狗", "兔子", "仓鼠"});
            dict.put("serviceTypes", new String[]{"洗澡", "美容", "训练", "寄养"});
            dict.put("genders", new String[]{"公", "母"});
            dict.put("petStatus", new String[]{"待售", "已售", "寄养中"});
            return Result.success(dict);
        } catch (Exception e) {
            log.error("获取字典信息失败", e);
            return Result.error("获取字典信息失败");
        }
    }

    @Operation(summary = "获取系统统计信息（宠物数量、销售额等）")
    @GetMapping("/statistics")
    public Result<?> getStatistics() {
        try {
            Map<String, Object> statistics = new HashMap<>();
            statistics.put("petCount", 0);
            statistics.put("totalSales", 0);
            statistics.put("userCount", 0);
            statistics.put("orderCount", 0);
            return Result.success(statistics);
        } catch (Exception e) {
            log.error("获取统计信息失败", e);
            return Result.error("获取统计信息失败");
        }
    }
}
